package cqut.wys.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/unsentEmail")
public class UnsentEmailController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @GetMapping("/add")
    public String add() {
        String sql;
        for (int i = 0; i < 100; i++) {
            sql ="INSERT INTO `queue`.`t_unsent_email` (`ts`) VALUES ('"+ LocalDateTime.now() +"')";
            System.out.println(sql);
            jdbcTemplate.execute(sql);
        }
        return "aaa";
    }

    @GetMapping("/update")
    public String update() {
        new Thread(() -> {
            while (true) {
                new Thread(() -> {
                    jdbcTemplate.execute("update t_unsent_email set `status`= 'claimed', `owner`=CONNECTION_ID() where `owner`=0 AND `status`='unsent' limit 1");
                    List<Map<String, Object>> list1 = jdbcTemplate.queryForList("SELECT * FROM t_unsent_email where `owner`=CONNECTION_ID() AND `status`= 'claimed'");
                    List<Map<String, Object>> list2 = jdbcTemplate.queryForList("select CONNECTION_ID()");
                    System.out.println("update: " + list2);
                }).start();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                jdbcTemplate.execute("UPDATE t_unsent_email set `owner`=0,`status`='unsent' where `status`='claimed' and ts < CURRENT_TIMESTAMP - INTERVAL 10 MINUTE");
            }
        }).start();
        return "uuu";
    }

    @GetMapping("/update1")
    public String update1() {
        new Thread(() -> {
            while (true) {
                new Thread(() -> {
                    jdbcTemplate.execute("update t_unsent_email set `status`= 'claimed', `owner`=CONNECTION_ID() where `owner`=0 AND `status`='unsent' limit 1");
                    List<Map<String, Object>> list1 = jdbcTemplate.queryForList("SELECT * FROM t_unsent_email where `owner`=CONNECTION_ID() AND `status`= 'claimed'");
                    List<Map<String, Object>> list2 = jdbcTemplate.queryForList("select CONNECTION_ID()");
                    System.out.println("update1: " + list2);
                }).start();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                jdbcTemplate.execute("UPDATE t_unsent_email set `owner`=0,`status`='unsent' where `status`='claimed' and ts < `CURRENT_TIMESTAMP` - INTERVAL 10 MINUTE");
            }
        }).start();
        return "uuu1";
    }
}
